﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Com.Maiyoule.GsmSms.Components {
    class DataCodingScheme {
        //bit 5
        private Int16 compressed=0;
        /// <summary>
        /// 是否GSM标准压缩
        /// </summary>
        public Int16 Compressed {
            get { return compressed; }
            set { compressed = value; }
        }

        private Int16 classType=0;
        /// <summary>
        /// 消息级别类型
        /// </summary>
        public Int16 ClassType {
            get { return classType; }
            set { classType = value; }
        }

        //bit 1 bit0
        private Int16 classLevel=0;
        /// <summary>
        /// 消息级别
        /// </summary>
        public Int16 ClassLevel {
            get { return classLevel; }
            set { classLevel = value; }
        }

        private Int16 coding=1;
        /// <summary>
        /// 编码
        /// </summary>
        public Int16 Coding {
            get { return coding; }
            set { coding = value; }
        }

        /// <summary>
        /// 获取据编码方案
        /// </summary>
        /// <returns>编码方案值（16进制编码）</returns>
        public string GetDCS() {
            /*
             Bit No.7与Bit No.6：
一般设置为00

Bit No.5：
0-文本未压缩
1-文本用GSM标准压缩算法压缩

Bit No.4：
0-指示Bit No.1 Bit No.0为保留位，不含信息类型信息
1-指示Bit No.1 Bit No.0含信息类型信息

Bit No.3与Bit No.2：
00-默认的字符集，每字符占7bit，此时最大可发送160字符
01-8bit，此时最大可发送140字符
10-USC2（16bit），发送双字节字符集
11-预留

Bit N0.1与Bit No.0：
00-Class 0，短消息直接显示在屏幕上
01-Class 1，
10-Class 2（SIM卡特定信息），
11-Class 3
             */
            string result = "00";
            result += this.compressed == 1 ? "1" : "0";
            result += this.classType == 1 ? "1" : "0";
            result += Util.Oct2BinString(this.coding).PadLeft(2,'0');
            result += Util.Oct2BinString(this.classLevel).PadLeft(2, '0');
            return Util.BinString2HexString(result).PadLeft(2,'0');
        }
        /// <summary>
        /// 设置编码方案
        /// </summary>
        /// <param name="data">方案值（16进制）</param>
        public void SetDCS(string data) {
            string binstr = Util.HexString2BinString(data);

            //丢弃前2个

            //压缩
            if (binstr.Substring(2, 1) == "0") {
                this.compressed = 0;
            } else {
                this.compressed = 1;
            }
            //指示
            if (binstr.Substring(3, 1) == "0") {
                this.classType = 0;
            } else {
                this.classType = 1;
               }
            //字符集
            this.coding = Util.BinString2Oct(binstr.Substring(4,2));
            this.classLevel = Util.BinString2Oct(binstr.Substring(6,2));

        }



    }
}
